import { createStore, combineReducers, applyMiddleware } from 'redux'
import carReducer from './car/carReducer'
import userReducer from './user/userReducer'
import searchReducer from './search/search'
import { useSelector, TypedUseSelectorHook } from 'react-redux'
import reduxLogger from 'redux-logger'
import { persistStore, persistReducer } from 'redux-persist'
import storage from 'redux-persist/lib/storage' // defaults to localStorage for web

const reducer = combineReducers({
	car: carReducer,
	user: userReducer,
	search: searchReducer
})

const persistConfig = {
  key: 'root',
  storage,
	whitelist: ['car', 'search', 'user']
}

const persistedReducer = persistReducer(persistConfig, reducer)

export const store = createStore(persistedReducer, applyMiddleware(reduxLogger))
export const persistor = persistStore(store)
export type RootState = ReturnType<typeof store.getState>
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector